﻿Public Class Form4
    Dim AnswerNo As Integer         ' 圖形出現的正確位置編號
    Dim ButtonNo As Integer         ' 玩家按下的按鈕位置編號
    Dim Count As Integer            ' 執行的總次數
    Dim GuessedCount As Integer     ' 猜中的次數
    Dim IsGuessed As Boolean        ' 記錄是否猜中
    Dim RndObj As New Random        ' 亂數物件

    Private Sub ShowResult()
        Dim s As String
        s = vbNewLine & "命中率 " & (GuessedCount / Count).ToString("0.00%") & vbNewLine
        s &= "猜中次數 " & GuessedCount & vbNewLine
        s &= "總次數 " & Count
        If IsGuessed Then
            MessageBox.Show("猜對了" & s, "結果", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("猜錯了" & s, "結果", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If
    End Sub

    Private Sub CleanButtonImage()
        Button1.Image = Nothing
        Button2.Image = Nothing
        Button3.Image = Nothing
    End Sub

    Private Sub CheckShowPicture()

        ' 隨機產生圖形出現的位置 1 ~ 3
        AnswerNo = RndObj.Next(1, 4)

        ' 在指定的位置顯示圖片
        Select Case AnswerNo
            Case 1
                Button1.Image = My.Resources.snowman
            Case 2
                Button2.Image = My.Resources.snowman
            Case 3
                Button3.Image = My.Resources.snowman
        End Select

        ' 累計執行次數
        Count += 1

        ' 判斷並記錄猜對與否
        If AnswerNo = ButtonNo Then
            GuessedCount += 1
            IsGuessed = True
        Else
            IsGuessed = False
        End If

        ' 顯示結果
        Call ShowResult()

        ' 清除按鈕上的圖片
        Call CleanButtonImage()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ButtonNo = 1
        Call CheckShowPicture()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ButtonNo = 2
        Call CheckShowPicture()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ButtonNo = 3
        Call CheckShowPicture()
    End Sub
End Class